/**
 * 
 */
package info.niwota.home.fm;

import info.niwota.home.R;

import java.io.IOException;

import x.java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * A simple text pad
 * 
 * @author qiangli
 * 
 */
public class WordActivity extends Activity implements OnClickListener {

	private File file;
	private EditText text;

	private void init() {
		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.word);
		getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.editor);

		Intent i = getIntent();
		Uri uri = i.getData();
		if (uri == null) {
			Toast.makeText(this, R.string.text_missing_uri, Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		text = (EditText) findViewById(R.id.word_text);

		final String p = uri.getPath();
		//
		CharSequence title = "";
		file = new File(p);
		boolean canRead = file.canRead();
		boolean isFolder = file.isDirectory();
		boolean canWrite = file.canWrite();
		if (isFolder) {
			String name = Utils.createName("text-", ".txt");
			file = new File(file, name);
			title = getString(R.string.app_name_word, name);
		} else {
			if (canRead) {
				// assume text is short, otherwise we may need a separate loader
				// thread
				// TODO
				String t = file.getContent();
				text.setText(t);
				title = getString(R.string.app_name_word, file.getName());
			}
		}
		//
		setTitle(title);
		//
		String act = i.getAction();
		boolean edit = (act != null && act.equals(Intent.ACTION_EDIT));
		View buttons = findViewById(R.id.word_buttons);
		buttons.setVisibility(edit ? View.VISIBLE : View.GONE);
		//
		final Button saveBtn = (Button) findViewById(R.id.word_save);
		saveBtn.setOnClickListener(this);
		saveBtn.setEnabled(canWrite);

		final Button discardBtn = (Button) findViewById(R.id.word_discard);
		discardBtn.setOnClickListener(this);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//
		init();
	}

	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.word_save:
			doSave();
			break;
		case R.id.word_discard:
			doDiscard();
			break;
		}
	}

	private void doDiscard() {
		setResult(RESULT_CANCELED);
		finish();
	}

	private void doSave() {
		try {
			String t = text.getText().toString();
			if (TextUtils.isEmpty(t)) {
				return;
			}
			file.setContent(t);
			Intent data = new Intent();
			data.setData(Uri.parse(file.toURI().toString()));
			setResult(RESULT_OK, data);
		} catch (IOException e) {
			Toast.makeText(WordActivity.this, e.getMessage(),
					Toast.LENGTH_SHORT).show();
		}
		finish();
	}
}
